package com.lxf.clouduser.config;

import com.lxf.cloudfeign.client.configsClient.ConfigClient;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.StringCodec;
import org.redisson.config.Config;
import org.redisson.config.ReadMode;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author 罗兴发
 * @data 2025/8/6 16:01
 * @description：分布式锁配置文件
 */
@Component
@Slf4j
public class RedissonConfig {
    @Resource
    private ConfigClient configClient;

    private int timeout = 2000;
    private int scanInterval = 60000;
    private static String ADDRESS_PREFIX = "redis://";

    @Bean
    public RedissonClient redisson() {
        String redissonServerNodes = configClient.getRedissonServerNodes();
        String[] nodes = redissonServerNodes.split(",");
        //redisson版本是3.5，集群的ip前面要加上“redis://”，不然会报错，3.2版本可不加
        for(int i=0;i<nodes.length;i++){
            nodes[i] = ADDRESS_PREFIX+nodes[i];
        }
        Config config = new Config();
        //设置
        config.setCodec(new StringCodec())
                //这是用的集群server
                .useClusterServers()
                //设置集群状态扫描时间
                .setScanInterval(scanInterval)
                .addNodeAddress(nodes)
//                .setPassword(password)
                .setReadMode(ReadMode.MASTER);;
        return Redisson.create(config);
    }
}
